home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4348 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: mudskipper.cac.psu.edu!user
  2. From: fcusack@tdx.org (frank.)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: simple code, argc, argv, strcmp()
  5. Date: Sat, 03 Feb 1996 16:45:28 -0400
  6. Organization: Penn State University, Center for Academic Computing
  7. Message-ID: <fcusack-0302961645280001@mudskipper.cac.psu.edu>
  8. References: <11f7cc$17261a.3b3@daprez> <4etj7c$bma@news.iag.net> <fcusack-0202961621470001@mudskipper.cac.psu.edu>
  9. NNTP-Posting-Host: mudskipper.cac.psu.edu
  10.  
  11. In article <fcusack-0202961621470001@mudskipper.cac.psu.edu>,
  12. fcusack@tdx.org (frank.) wrote:
  13.  
  14. > In article <4etj7c$bma@news.iag.net>, jatmon@iag.net (John R Buchan) wrote:
  15. > > In article <11f7cc$17261a.3b3@daprez>, otisg@panther.middlebury.edu says...
  16. > > >
  17. > > >
  18. > > >#include <stdio.h>
  19. > > >#include <stdlib.h>
  20. > > >#include <string.h>
  21. > > >
  22. > > >int main (int argc, char **argv) {
  23. > > >
  24. > > >  void Usage   (void);
  25. > > >  void Encode (int, char **);
  26. > > >  void Decode (int, char **);
  27. > > >
  28. > > >  if (!strcmp(argv[1],"-d") || !strcmp(argv[1],"-e")) {
  29. > > >    Usage();
  30. > > >  }
  31. > > <snip>
  32. > > 
  33. > > I assume you mean this to call Usage if argv[1] is not "-e" and not "-d"?
  34. > > Change the || to &&.  
  35.  
  36. I mistakenly wrote:
  37.  
  38. > The && test would _never_ pass. argv[1] could never be both "-d" and "-e".
  39. > The || is correct here.
  40.  
  41. Yes, the && test would never pass, but the || test is bad also. The ||
  42. will pass if you pass "-d" or "-e". So then you will call Usage()
  43. incorrectly. The test should be:
  44.  
  45.    if (!(!strcmp(argv[1], "-d") || !strcmp(argv[1], "-e"))) {
  46.       Usage();
  47.    }
  48.  
  49. > >Of course, if no argument was passed, who knows what
  50. > > will happen here.  You should always test argc first to be certain that 
  51. > > the argv element exists.
  52.  
  53. and I also incorrectly wrote:
  54.  
  55. > argv[1] is always guaranteed to exist. It is simply NULL if there were no
  56. > arguments.
  57.  
  58. Dan Pop has the poop on this one in his followup. eg, on the Mac, argc is
  59. probably always zero, thus argv[1] is undefined.
  60. ~Frank
  61.  -- I am Pentium of Borg.  Division is futile.  You will be approximated. --
  62.  --   If you build it, they will come --> http://www.tdx.org/~fcusack/    --
  63.  -- PGP key fingerprint: 01 C0 C0 B9 CC 78 67 0F  3F 64 80 65 8B 0F F9 EA --
  64.